import * as echarts from '../../plugin/ec-canvas/echarts';
// pages/login/login.js
//获取应用实例
const app = getApp()
const util = require('../../../utils/util.js')
var chart1 = null
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loadding : 0,
    files:[],
    dealFiles:[],
    imgUrl:[],
    hide:false,
    checkItemListData: [],
    photo:[],
    itemIndex:0,
    detailIndex:0,
    type:0,
    contentIndex: -1
  },
  onLoad: function(options) {
    let address = options.address 
    let recordContent = options.recordContent
    let realname = app.globalData.realname;
    let screenHeight = wx.getSystemInfoSync().windowHeight
    let detailIndex = options.detailIndex
    let itemIndex = options.itemIndex
    let category = options.category 
    let level = options.level 
    //type 1代表安全现场 2代表巡检 3代表巡检更新 
    let type = options.type
    let that = this 
    if(type == 2){
      //巡检
      let checkItemList = JSON.parse(options.checkItemList)
      that.setData({
        checkItemList:checkItemList,
        item:checkItemList[itemIndex].item,
        itemContent:checkItemList[itemIndex].contentList[detailIndex].content
      })
    }else if(type == 3){
      let photo = JSON.parse(options.photo)
      let item = options.item
      let content = options.content
      that.setData({
        photo:photo,
        item:item,
        itemContent:content
      })
    }else if(type == 4){
      //值周
      let checkItemList = JSON.parse(options.checkItemList)
      let dutyCategory = options.dutyCategory
      let deduction = checkItemList[detailIndex].deduction
      if(deduction == 0){
        deduction = "无扣分"
      }else{
        deduction = '- '+deduction
      }
      that.setData({
        checkItemList:checkItemList,
        dutyCategory :dutyCategory,
        dutyItem : checkItemList[detailIndex].item,
        deduction: deduction
      })
    } else if(type == 6) {
      let contentIndex = options.contentIndex
      this.setData({
        contentIndex: contentIndex
      })
    }
    that.setData({
      address : address,
      recordContent : recordContent,
      realname:realname,
      category:category,
      level:level,
      screenHeight:screenHeight - 90,
      itemIndex:itemIndex,
      detailIndex:detailIndex,
      type:type,
    })
    setInterval(function(){
      let datetime = util.formatTime(new Date());
      that.setData({
        datetime : datetime
      })
    },1000)
  },

  /**
 * params
 * @text           需要绘制的文本字符
 * @startX         第一行文本的起始X坐标
 * @startY         第一行文本的起始Y坐标
 * @lineHeight     文本行高
 * @MAX_WIDTH      单行文字最大宽度，超过临界值自动换行
 * return rowLength  返回绘制文本的行数
**/
  transformation:function(data){
    var ctx = wx.createCanvasContext('myCanvas')
    let rowArr = []
    let tempArr = []
    let tempRowStr
    let allAtr = data.split('');
    let rowStrArr = []; // 每一行的文字数组
    for (let i = 0; i < allAtr.length; i++) {
      const currentStr = allAtr[i];
      rowStrArr.push(currentStr);
      const rowStr = rowStrArr.join('');
      ctx.setFontSize(13)
      if (ctx.measureText(rowStr).width > 125) {
        rowStrArr.pop(); // 删除最后一个
        rowArr.push(rowStrArr.join('')); // 完成一行
        rowStrArr = [currentStr];
        if(i === allAtr.length - 1){
          tempArr.push(currentStr)
          tempRowStr = tempArr.join('');
          rowArr.push(tempRowStr); // 完成一行
        }else{
          rowStrArr = [currentStr];
        }
        continue;
      }
      // 最后一个字母 直接添加到一行
      if (i === allAtr.length - 1) {
        rowArr.push(rowStr); // 完成一行
      }
    }
    return rowArr
  },

  //安全现场
  takePhoto() {
    var that = this;
    const ctx = wx.createCameraContext()
    console.log(ctx)
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        wx.showLoading({
          title: "图片上传中",
          mask: true
        })
        let tempImg = res.tempImagePath
        console.log("tempImg",tempImg)
        //新增图片水印
        let datetime = util.formatTime(new Date());
        wx.getImageInfo({
            src: tempImg,
            success:function(res2){
              var width = res2.width 
              var height = res2.height
              //获取屏幕宽度
              let screenWidth = wx.getSystemInfoSync().windowWidth
              console.log("screenWidth",screenWidth)
              //处理一下图片的宽高的比例
              let ratio = width/height
              width = screenWidth
              height = width/ratio
              that.setData({
                imageWidth: width,
                imageHeight: height,
              })
              var ctx = wx.createCanvasContext('myCanvas')
              if(that.data.type == 1){
                //安全现场
                let rowArr = that.transformation(that.data.recordContent)
                let k = 0
                if(rowArr.length < 3){
                  if(rowArr.length == 0){
                    k = 1
                  }else{
                    k = rowArr.length
                  }
                }else{
                  k = 3
                }
                ctx.drawImage(res2.path,0,0,width,height)
                ctx.rect(10, (height-230+(3-k)*20), 207, 50)
                ctx.setFillStyle('#396EB0')
                ctx.fill()
                ctx.beginPath()
                ctx.rect(10, (height-180+(3-k)*20), 207, 170-(3-k)*20)
                ctx.setFillStyle('#EAECED')
                ctx.fill()
                ctx.beginPath()
                ctx.setFontSize(16)
                ctx.setFillStyle('#ffffff')
                ctx.fillText('隐患记录', 20, (height-210+(3-k)*20))
                ctx.beginPath()
                ctx.setFontSize(13)
                ctx.setFillStyle('#ffffff')
                ctx.fillText(datetime, 80, (height-188+(3-k)*20))
                ctx.beginPath()
                ctx.setFontSize(13)
                ctx.setFillStyle('black')
                ctx.fillText('上', 20, (height-160+(3-k)*20))
                ctx.fillText('报', 40, (height-160+(3-k)*20))
                ctx.fillText('人', 60, (height-160+(3-k)*20))
                ctx.fillText(app.globalData.realname, 85, (height-160+(3-k)*20))
                ctx.fillText('地', 20, (height-135+(3-k)*20))
                ctx.fillText('点', 60, (height-135+(3-k)*20))
                let rowArr2 = that.transformation(that.data.address)
                for (let i = 0; i < rowArr2.length; i++) {
                  if(i == 0){
                    if(rowArr2.length > 1){
                      ctx.fillText(rowArr2[i].slice(0,9).concat("..."), 85, (height-135+(3-k)*20)+i*20);
                    }else{
                      ctx.fillText(rowArr2[i], 85, (height-135+(3-k)*20)+i*20)
                    }
                  }
                }
                ctx.fillText('隐患类别', 20, (height-110+(3-k)*20));
                ctx.fillText(that.data.category, 85, (height-110+(3-k)*20))
                ctx.fillText('隐患等级', 20, (height-85+(3-k)*20));
                ctx.fillText(that.data.level, 85, (height-85+(3-k)*20))
                ctx.fillText('隐患描述', 20, (height-60+(3-k)*20));
                for (let i = 0; i < rowArr.length; i++) {
                    if(i == 0){
                      ctx.fillText(rowArr[i], 85, (height-60+(3-k)*20)+i*20);
                    }else if(i == 1){
                      ctx.fillText(rowArr[i], 85, (height-60+(3-k)*20)+i*20);
                    }else if(i == 2){
                      if(rowArr.length > 3){
                        ctx.fillText(rowArr[i].slice(0,9).concat("..."), 85, (height-60+(3-k)*20)+i*20);
                      }else{
                        ctx.fillText(rowArr[i], 85, (height-60+(3-k)*20)+i*20);
                      }
                    }
                }
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-157+(3-k)*20))
                ctx.lineTo(212, (height-157+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-132+(3-k)*20))
                ctx.lineTo(212, (height-132+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-107+(3-k)*20))
                ctx.lineTo(212, (height-107+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-82+(3-k)*20))
                ctx.lineTo(212, (height-82+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-57+(3-k)*20))
                ctx.lineTo(212, (height-57+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-37+(3-k)*20))
                ctx.lineTo(212, (height-37+(3-k)*20))
                ctx.stroke() 
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-17+(3-k)*20))
                ctx.lineTo(212, (height-17+(3-k)*20))
                ctx.stroke() 
                ctx.draw()
                setTimeout(function(){
                  wx.canvasToTempFilePath({
                      canvasId: 'myCanvas',
                      success: function (res3) {
                          var tempFilePath = res3.tempFilePath;
                          console.log(tempFilePath)
                          //图片上传功能
                          wx.uploadFile({
                            url: app.inspectUrl + '/WeChat/weChatSystem/fileUpload',
                            // filePath: res.tempFilePaths[i],
                            filePath: tempFilePath,
                            name: 'file',
                            header: {
                              "Content-Type": "multipart/form-data"
                            },
                            success: res4 => {
                              console.log("res4",res4.data)
                              if (res.statusCode == 500) {
                                wx.showToast({
                                  title: "图片上传失败",
                                  image: "/images/error.png"
                                })
                              } else{
                                var pages = getCurrentPages();
                                var prevPage = pages[pages.length - 2];
                                if(wx.getStorageSync('fileAndImg') == null || wx.getStorageSync('fileAndImg') == ""){
                                  console.log("------null-------")
                                  wx.setStorageSync('fileAndImg', {
                                    files : that.data.files.concat(tempFilePath),
                                    imgUrl : that.data.imgUrl.concat(res4.data),
                                  })
                                  prevPage.setData({
                                    files: that.data.files.concat(tempFilePath),
                                    imgUrl: that.data.imgUrl.concat(res4.data),
                                    hide : false
                                  })
                                }else{
                                  console.log("------!!!null-------")
                                  prevPage.setData({
                                    files: wx.getStorageSync('fileAndImg').files.concat(tempFilePath),
                                    imgUrl: wx.getStorageSync('fileAndImg').imgUrl.concat(res4.data),
                                    hide : false
                                  })
                                  wx.setStorageSync('fileAndImg', {
                                    files : wx.getStorageSync('fileAndImg').files.concat(tempFilePath),
                                    imgUrl : wx.getStorageSync('fileAndImg').imgUrl.concat(res4.data)
                                  })
                                  if(wx.getStorageSync('fileAndImg').files.length == 3){
                                    prevPage.setData({
                                      hide : true
                                    })
                                  }
                                }
                                wx.navigateBack({
                                  delta: 1
                                })
                                wx.showToast({
                                  title: "上传成功"
                                })
                              }
                            },
                            fail: res4 => {
                              console.log(res4)
                              wx.showToast({
                                title: "图片上传失败",
                                image: "/images/error.png"
                              })
                            }
                          })
                      },
                      fail: function (res3) {
                          console.log(res3);
                      }
                  });
                 },500);
              }else if(that.data.type == 2){
                //巡检记录
                let rowArr = that.transformation(that.data.recordContent)
                let k = 0
                if(rowArr.length < 3){
                  if(rowArr.length == 0){
                    k = 1
                  }else{
                    k = rowArr.length
                  }
                }else{
                  k = 3
                }
                ctx.drawImage(res2.path,0,0,width,height)
                ctx.rect(10, (height-230+(3-k)*20), 207, 50)
                ctx.setFillStyle('#396EB0')
                ctx.fill()
                ctx.beginPath()
                ctx.rect(10, (height-180+(3-k)*20), 207, 170-(3-k)*20)
                ctx.setFillStyle('#EAECED')
                ctx.fill()
                ctx.beginPath()
                ctx.setFontSize(16)
                ctx.setFillStyle('#ffffff')
                ctx.fillText('巡检记录', 20, (height-210+(3-k)*20))
                ctx.beginPath()
                ctx.setFontSize(13)
                ctx.setFillStyle('#ffffff')
                ctx.fillText(datetime, 80, (height-188+(3-k)*20))
                ctx.beginPath()
                ctx.setFontSize(13)
                ctx.setFillStyle('black')
                ctx.fillText('上', 20, (height-160+(3-k)*20))
                ctx.fillText('报', 40, (height-160+(3-k)*20))
                ctx.fillText('人', 60, (height-160+(3-k)*20))
                ctx.fillText(app.globalData.realname, 85, (height-160+(3-k)*20))
                ctx.fillText('地', 20, (height-135+(3-k)*20))
                ctx.fillText('点', 60, (height-135+(3-k)*20))
                let rowArr2 = that.transformation(that.data.address)
                for (let i = 0; i < rowArr2.length; i++) {
                  if(i == 0){
                    if(rowArr2.length > 1){
                      ctx.fillText(rowArr2[i].slice(0,9).concat("..."), 85, (height-135+(3-k)*20)+i*20);
                    }else{
                      ctx.fillText(rowArr2[i], 85, (height-135+(3-k)*20)+i*20)
                    }
                  }
                }
                ctx.fillText('巡检类别', 20, (height-110+(3-k)*20));
                let rowArr3 = that.transformation(that.data.item)
                for (let i = 0; i < rowArr3.length; i++) {
                  if(i == 0){
                    if(rowArr3.length > 1){
                      ctx.fillText(rowArr3[i].slice(0,9).concat("..."), 85, (height-110+(3-k)*20)+i*20);
                    }else{
                      ctx.fillText(rowArr3[i], 85, (height-110+(3-k)*20)+i*20)
                    }
                  }
                }
                ctx.fillText('巡检项目', 20, (height-85+(3-k)*20));
                let rowArr4 = that.transformation(that.data.itemContent)
                for (let i = 0; i < rowArr4.length; i++) {
                  if(i == 0){
                    if(rowArr4.length > 1){
                      ctx.fillText(rowArr4[i].slice(0,9).concat("..."), 85, (height-85+(3-k)*20)+i*20);
                    }else{
                      ctx.fillText(rowArr4[i], 85, (height-85+(3-k)*20)+i*20)
                    }
                  }
                }
                ctx.fillText('隐患描述', 20, (height-60+(3-k)*20));
                console.log("rowArr",rowArr)
                for (let i = 0; i < rowArr.length; i++) {
                    if(i == 0){
                      ctx.fillText(rowArr[i], 85, (height-60+(3-k)*20)+i*20);
                    }else if(i == 1){
                      ctx.fillText(rowArr[i], 85, (height-60+(3-k)*20)+i*20);
                    }else if(i == 2){
                      if(rowArr.length > 3){
                        ctx.fillText(rowArr[i].slice(0,9).concat("..."), 85, (height-60+(3-k)*20)+i*20);
                      }else{
                        ctx.fillText(rowArr[i], 85, (height-60+(3-k)*20)+i*20);
                      }
                    }
                }
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-157+(3-k)*20))
                ctx.lineTo(212, (height-157+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-132+(3-k)*20))
                ctx.lineTo(212, (height-132+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-107+(3-k)*20))
                ctx.lineTo(212, (height-107+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-82+(3-k)*20))
                ctx.lineTo(212, (height-82+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-57+(3-k)*20))
                ctx.lineTo(212, (height-57+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-37+(3-k)*20))
                ctx.lineTo(212, (height-37+(3-k)*20))
                ctx.stroke() 
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-17+(3-k)*20))
                ctx.lineTo(212, (height-17+(3-k)*20))
                ctx.stroke() 
                ctx.draw()
                setTimeout(function(){
                  wx.canvasToTempFilePath({
                      canvasId: 'myCanvas',
                      success: function (res3) {
                          var tempFilePath = res3.tempFilePath;
                          console.log('temp', tempFilePath)
                          //图片上传功能
                          wx.uploadFile({
                            url: app.inspectUrl + '/WeChat/weChatSystem/fileUpload',
                            // filePath: res.tempFilePaths[i],
                            filePath: tempFilePath,
                            name: 'file',
                            header: {
                              "Content-Type": "multipart/form-data"
                            },
                            success: res4 => {
                              console.log("res4",res4.data)
                              if (res.statusCode == 500) {
                                wx.showToast({
                                  title: "图片上传失败",
                                  image: "/images/error.png"
                                })
                              } else{
                                var pages = getCurrentPages();
                                var prevPage = pages[pages.length - 2];
                                var itemIndex = that.data.itemIndex
                                var detailIndex = that.data.detailIndex
                                that.data.checkItemList[itemIndex].contentList[detailIndex].dealFiles.push(tempFilePath)
                                that.data.checkItemList[itemIndex].contentList[detailIndex].imageUrl.push(res4.data)
                                that.data.checkItemList[itemIndex].contentList[detailIndex].imageNum++
                                prevPage.setData({
                                  checkItemList: that.data.checkItemList ,
                                })
                                if(that.data.checkItemList[itemIndex].contentList[detailIndex].dealFiles.length == 3){
                                  that.data.checkItemList[itemIndex].contentList[detailIndex].hide = true
                                  prevPage.setData({
                                    checkItemList: that.data.checkItemList,
                                  })
                                }
                                wx.navigateBack({
                                  delta: 1
                                })
                                wx.showToast({
                                  title: "上传成功"
                                })
                              }
                            },
                            fail: res4 => {
                              console.log(res4)
                              wx.showToast({
                                title: "图片上传失败",
                                image: "/images/error.png"
                              })
                            }
                          })
                      },
                      fail: function (res3) {
                          console.log(res3);
                      }
                  });
                 },500);
              }else if(that.data.type == 3){
                //巡检更新 
                let rowArr = that.transformation(that.data.recordContent)
                let k = 0
                if(rowArr.length < 3){
                  if(rowArr.length == 0){
                    k = 1
                  }else{
                    k = rowArr.length
                  }
                }else{
                  k = 3
                }
                ctx.drawImage(res2.path,0,0,width,height)
                ctx.rect(10, (height-230+(3-k)*20), 207, 50)
                ctx.setFillStyle('#396EB0')
                ctx.fill()
                ctx.beginPath()
                ctx.rect(10, (height-180+(3-k)*20), 207, 170-(3-k)*20)
                ctx.setFillStyle('#EAECED')
                ctx.fill()
                ctx.beginPath()
                ctx.setFontSize(16)
                ctx.setFillStyle('#ffffff')
                ctx.fillText('巡检记录', 20, (height-210+(3-k)*20))
                ctx.beginPath()
                ctx.setFontSize(13)
                ctx.setFillStyle('#ffffff')
                ctx.fillText(datetime, 80, (height-188+(3-k)*20))
                ctx.beginPath()
                ctx.setFontSize(13)
                ctx.setFillStyle('black')
                ctx.fillText('上', 20, (height-160+(3-k)*20))
                ctx.fillText('报', 40, (height-160+(3-k)*20))
                ctx.fillText('人', 60, (height-160+(3-k)*20))
                ctx.fillText(app.globalData.realname, 85, (height-160+(3-k)*20))
                ctx.fillText('地', 20, (height-135+(3-k)*20))
                ctx.fillText('点', 60, (height-135+(3-k)*20))
                let rowArr2 = that.transformation(that.data.address)
                for (let i = 0; i < rowArr2.length; i++) {
                  if(i == 0){
                    if(rowArr2.length > 1){
                      ctx.fillText(rowArr2[i].slice(0,9).concat("..."), 85, (height-135+(3-k)*20)+i*20);
                    }else{
                      ctx.fillText(rowArr2[i], 85, (height-135+(3-k)*20)+i*20)
                    }
                  }
                }
                ctx.fillText('巡检类别', 20, (height-110+(3-k)*20));
                let rowArr3 = that.transformation(that.data.item)
                for (let i = 0; i < rowArr3.length; i++) {
                  if(i == 0){
                    if(rowArr3.length > 1){
                      ctx.fillText(rowArr3[i].slice(0,9).concat("..."), 85, (height-110+(3-k)*20)+i*20);
                    }else{
                      ctx.fillText(rowArr3[i], 85, (height-110+(3-k)*20)+i*20)
                    }
                  }
                }
                ctx.fillText('巡检项目', 20, (height-85+(3-k)*20));
                let rowArr4 = that.transformation(that.data.itemContent)
                for (let i = 0; i < rowArr4.length; i++) {
                  if(i == 0){
                    if(rowArr4.length > 1){
                      ctx.fillText(rowArr4[i].slice(0,9).concat("..."), 85, (height-85+(3-k)*20)+i*20);
                    }else{
                      ctx.fillText(rowArr4[i], 85, (height-85+(3-k)*20)+i*20)
                    }
                  }
                }
                ctx.fillText('隐患描述', 20, (height-60+(3-k)*20));
                console.log("rowArr",rowArr)
                for (let i = 0; i < rowArr.length; i++) {
                    if(i == 0){
                      ctx.fillText(rowArr[i], 85, (height-60+(3-k)*20)+i*20);
                    }else if(i == 1){
                      ctx.fillText(rowArr[i], 85, (height-60+(3-k)*20)+i*20);
                    }else if(i == 2){
                      if(rowArr.length > 3){
                        ctx.fillText(rowArr[i].slice(0,9).concat("..."), 85, (height-60+(3-k)*20)+i*20);
                      }else{
                        ctx.fillText(rowArr[i], 85, (height-60+(3-k)*20)+i*20);
                      }
                    }
                }
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-157+(3-k)*20))
                ctx.lineTo(212, (height-157+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-132+(3-k)*20))
                ctx.lineTo(212, (height-132+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-107+(3-k)*20))
                ctx.lineTo(212, (height-107+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-82+(3-k)*20))
                ctx.lineTo(212, (height-82+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-57+(3-k)*20))
                ctx.lineTo(212, (height-57+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-37+(3-k)*20))
                ctx.lineTo(212, (height-37+(3-k)*20))
                ctx.stroke() 
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-17+(3-k)*20))
                ctx.lineTo(212, (height-17+(3-k)*20))
                ctx.stroke() 
                ctx.draw()
                setTimeout(function(){
                  wx.canvasToTempFilePath({
                      canvasId: 'myCanvas',
                      success: function (res3) {
                          var tempFilePath = res3.tempFilePath;
                          //图片上传功能
                          wx.uploadFile({
                            url: app.inspectUrl + '/WeChat/weChatSystem/fileUpload',
                            // filePath: res.tempFilePaths[i],
                            filePath: tempFilePath,
                            name: 'file',
                            header: {
                              "Content-Type": "multipart/form-data"
                            },
                            success: res4 => {
                              console.log("res4",res4.data)
                              if (res.statusCode == 500) {
                                wx.showToast({
                                  title: "图片上传失败",
                                  image: "/images/error.png"
                                })
                              } else{
                                var pages = getCurrentPages();
                                var prevPage = pages[pages.length - 2];
                                let imgUrl = []
                                that.data.photo.push(app.imageUrl + res4.data)
                                imgUrl.push(res4.data)
                                prevPage.setData({
                                  photo: that.data.photo ,
                                  imgUrl : imgUrl
                                })
                                wx.navigateBack({
                                  delta: 1
                                })
                                wx.showToast({
                                  title: "上传成功"
                                })
                              }
                            },
                            fail: res4 => {
                              console.log(res4)
                              wx.showToast({
                                title: "图片上传失败",
                                image: "/images/error.png"
                              })
                            }
                          })
                      },
                      fail: function (res3) {
                          console.log(res3);
                      }
                  });
                },500);
              }else if(that.data.type == 4){
                //值周记录
                let rowArr = that.transformation(that.data.recordContent)
                let k = 0
                if(rowArr.length < 3){
                  if(rowArr.length == 0){
                    k = 1
                  }else{
                    k = rowArr.length
                  }
                }else{
                  k = 3
                }
                ctx.drawImage(res2.path,0,0,width,height)
                ctx.rect(10, (height-255+(3-k)*20), 207, 50)
                ctx.setFillStyle('#396EB0')
                ctx.fill()
                ctx.beginPath()
                ctx.rect(10, (height-205+(3-k)*20), 207, 195-(3-k)*20)
                ctx.setFillStyle('#EAECED')
                ctx.fill()
                ctx.beginPath()
                ctx.setFontSize(16)
                ctx.setFillStyle('#ffffff')
                ctx.fillText('值周记录', 20, (height-235+(3-k)*20))
                ctx.beginPath()
                ctx.setFontSize(13)
                ctx.setFillStyle('#ffffff')
                ctx.fillText(datetime, 80, (height-213+(3-k)*20))
                ctx.beginPath()
                ctx.setFontSize(13)
                ctx.setFillStyle('black')
                ctx.fillText('上', 20, (height-185+(3-k)*20))
                ctx.fillText('报', 40, (height-185+(3-k)*20))
                ctx.fillText('人', 60, (height-185+(3-k)*20))
                ctx.fillText(app.globalData.realname, 85, (height-185+(3-k)*20))
                ctx.fillText('地', 20, (height-160+(3-k)*20))
                ctx.fillText('点', 60, (height-160+(3-k)*20))
                let rowArr2 = that.transformation(that.data.address)
                for (let i = 0; i < rowArr2.length; i++) {
                  if(i == 0){
                    if(rowArr2.length > 1){
                      ctx.fillText(rowArr2[i].slice(0,9).concat("..."), 85, (height-160+(3-k)*20)+i*20);
                    }else{
                      ctx.fillText(rowArr2[i], 85, (height-160+(3-k)*20)+i*20)
                    }
                  }
                }
                ctx.fillText('值周类别', 20, (height-135+(3-k)*20));
                let rowArr3 = that.transformation(that.data.dutyCategory)
                for (let i = 0; i < rowArr3.length; i++) {
                  if(i == 0){
                    if(rowArr3.length > 1){
                      ctx.fillText(rowArr3[i].slice(0,9).concat("..."), 85, (height-135+(3-k)*20)+i*20);
                    }else{
                      ctx.fillText(rowArr3[i], 85, (height-135+(3-k)*20)+i*20)
                    }
                  }
                }
                ctx.fillText('值周项目', 20, (height-110+(3-k)*20));
                let rowArr4 = that.transformation(that.data.dutyItem)
                for (let i = 0; i < rowArr4.length; i++) {
                  if(i == 0){
                    if(rowArr4.length > 1){
                      ctx.fillText(rowArr4[i].slice(0,9).concat("..."), 85, (height-110+(3-k)*20)+i*20);
                    }else{
                      ctx.fillText(rowArr4[i], 85, (height-110+(3-k)*20)+i*20)
                    }
                  }
                }
                ctx.fillText('扣分情况', 20, (height-85+(3-k)*20));
                ctx.fillText(that.data.deduction, 85, (height-85+(3-k)*20));
                ctx.fillText('问题描述', 20, (height-60+(3-k)*20));
                console.log("rowArr",rowArr)
                for (let i = 0; i < rowArr.length; i++) {
                    if(i == 0){
                      ctx.fillText(rowArr[i], 85, (height-60+(3-k)*20)+i*20);
                    }else if(i == 1){
                      ctx.fillText(rowArr[i], 85, (height-60+(3-k)*20)+i*20);
                    }else if(i == 2){
                      if(rowArr.length > 3){
                        ctx.fillText(rowArr[i].slice(0,9).concat("..."), 85, (height-60+(3-k)*20)+i*20);
                      }else{
                        ctx.fillText(rowArr[i], 85, (height-60+(3-k)*20)+i*20);
                      }
                    }
                }
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-182+(3-k)*20))
                ctx.lineTo(212, (height-182+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-157+(3-k)*20))
                ctx.lineTo(212, (height-157+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-132+(3-k)*20))
                ctx.lineTo(212, (height-132+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-107+(3-k)*20))
                ctx.lineTo(212, (height-107+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-82+(3-k)*20))
                ctx.lineTo(212, (height-82+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-57+(3-k)*20))
                ctx.lineTo(212, (height-57+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-37+(3-k)*20))
                ctx.lineTo(212, (height-37+(3-k)*20))
                ctx.stroke() 
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-17+(3-k)*20))
                ctx.lineTo(212, (height-17+(3-k)*20))
                ctx.stroke() 
                ctx.draw()
                setTimeout(function(){
                  wx.canvasToTempFilePath({
                      canvasId: 'myCanvas',
                      success: function (res3) {
                          var tempFilePath = res3.tempFilePath;
                          //图片上传功能
                          wx.uploadFile({
                            url: app.inspectUrl + '/WeChat/weChatSystem/fileUpload',
                            // filePath: res.tempFilePaths[i],
                            filePath: tempFilePath,
                            name: 'file',
                            header: {
                              "Content-Type": "multipart/form-data"
                            },
                            success: res4 => {
                              console.log("res4",res4.data)
                              if (res.statusCode == 500) {
                                wx.showToast({
                                  title: "图片上传失败",
                                  image: "/images/error.png"
                                })
                              } else{
                                var pages = getCurrentPages();
                                var prevPage = pages[pages.length - 2];
                                var detailIndex = that.data.detailIndex
                                that.data.checkItemList[detailIndex].dealFiles.push(app.imageUrl + res4.data)
                                that.data.checkItemList[detailIndex].imgHttpUrl.push(app.imageUrl + res4.data)
                                that.data.checkItemList[detailIndex].imageNum++
                                prevPage.setData({
                                  checkItemList: that.data.checkItemList ,
                                })
                                if(that.data.checkItemList[detailIndex].dealFiles.length == 3){
                                  that.data.checkItemList[detailIndex].hide = true
                                  prevPage.setData({
                                    checkItemList: that.data.checkItemList,
                                  })
                                }
                                wx.navigateBack({
                                  delta: 1
                                })
                                wx.showToast({
                                  title: "上传成功"
                                })
                              }
                            },
                            fail: res4 => {
                              console.log(res4)
                              wx.showToast({
                                title: "图片上传失败",
                                image: "/images/error.png"
                              })
                            }
                          })
                      },
                      fail: function (res3) {
                          console.log(res3);
                      }
                  });
                 },500);
              }else if(that.data.type == 5){
                //安全现场,巡检 处理
                let rowArr = that.transformation(that.data.recordContent)
                let k = 0
                if(rowArr.length < 3){
                  if(rowArr.length == 0){
                    k = 1
                  }else{
                    k = rowArr.length
                  }
                }else{
                  k = 3
                }
                ctx.drawImage(res2.path,0,0,width,height)
                ctx.rect(10, (height-230+(3-k)*20), 207, 50)
                ctx.setFillStyle('#396EB0')
                ctx.fill()
                ctx.beginPath()
                ctx.rect(10, (height-180+(3-k)*20), 207, 170-(3-k)*20)
                ctx.setFillStyle('#EAECED')
                ctx.fill()
                ctx.beginPath()
                ctx.setFontSize(16)
                ctx.setFillStyle('#ffffff')
                ctx.fillText('隐患处理记录', 20, (height-210+(3-k)*20))
                ctx.beginPath()
                ctx.setFontSize(13)
                ctx.setFillStyle('#ffffff')
                ctx.fillText(datetime, 80, (height-188+(3-k)*20))
                ctx.beginPath()
                ctx.setFontSize(13)
                ctx.setFillStyle('black')
                ctx.fillText('上', 20, (height-160+(3-k)*20))
                ctx.fillText('报', 40, (height-160+(3-k)*20))
                ctx.fillText('人', 60, (height-160+(3-k)*20))
                ctx.fillText(app.globalData.realname, 85, (height-160+(3-k)*20))
                ctx.fillText('地', 20, (height-135+(3-k)*20))
                ctx.fillText('点', 60, (height-135+(3-k)*20))
                let rowArr2 = that.transformation(that.data.address)
                for (let i = 0; i < rowArr2.length; i++) {
                  if(i == 0){
                    if(rowArr2.length > 1){
                      ctx.fillText(rowArr2[i].slice(0,9).concat("..."), 85, (height-135+(3-k)*20)+i*20);
                    }else{
                      ctx.fillText(rowArr2[i], 85, (height-135+(3-k)*20)+i*20)
                    }
                  }
                }
                ctx.fillText('隐患类别', 20, (height-110+(3-k)*20));
                ctx.fillText(that.data.category, 85, (height-110+(3-k)*20))
                ctx.fillText('隐患等级', 20, (height-85+(3-k)*20));
                ctx.fillText(that.data.level, 85, (height-85+(3-k)*20))
                ctx.fillText('隐患描述', 20, (height-60+(3-k)*20));
                console.log("rowArr",rowArr)
                for (let i = 0; i < rowArr.length; i++) {
                    if(i == 0){
                      ctx.fillText(rowArr[i], 85, (height-60+(3-k)*20)+i*20);
                    }else if(i == 1){
                      ctx.fillText(rowArr[i], 85, (height-60+(3-k)*20)+i*20);
                    }else if(i == 2){
                      if(rowArr.length > 3){
                        ctx.fillText(rowArr[i].slice(0,9).concat("..."), 85, (height-60+(3-k)*20)+i*20);
                      }else{
                        ctx.fillText(rowArr[i], 85, (height-60+(3-k)*20)+i*20);
                      }
                    }
                }
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-157+(3-k)*20))
                ctx.lineTo(212, (height-157+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-132+(3-k)*20))
                ctx.lineTo(212, (height-132+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-107+(3-k)*20))
                ctx.lineTo(212, (height-107+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-82+(3-k)*20))
                ctx.lineTo(212, (height-82+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-57+(3-k)*20))
                ctx.lineTo(212, (height-57+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-37+(3-k)*20))
                ctx.lineTo(212, (height-37+(3-k)*20))
                ctx.stroke() 
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-17+(3-k)*20))
                ctx.lineTo(212, (height-17+(3-k)*20))
                ctx.stroke() 
                ctx.draw()
                setTimeout(function(){
                  wx.canvasToTempFilePath({
                      canvasId: 'myCanvas',
                      success: function (res3) {
                          var tempFilePath = res3.tempFilePath;
                          //图片上传功能
                          wx.uploadFile({
                            url: app.inspectUrl + '/WeChat/weChatSystem/fileUpload',
                            // filePath: res.tempFilePaths[i],
                            filePath: tempFilePath,
                            name: 'file',
                            header: {
                              "Content-Type": "multipart/form-data"
                            },
                            success: res4 => {
                              console.log("res4",res4.data)
                              if (res.statusCode == 500) {
                                wx.showToast({
                                  title: "图片上传失败",
                                  image: "/images/error.png"
                                })
                              } else{
                                var pages = getCurrentPages();
                                var prevPage = pages[pages.length - 2];
                                if(wx.getStorageSync('fileAndImg') == null || wx.getStorageSync('fileAndImg') == ""){
                                  console.log("------null-------")
                                  wx.setStorageSync('fileAndImg', {
                                    files : that.data.dealFiles.concat(tempFilePath),
                                    imgUrl : that.data.imgUrl.concat(res4.data),
                                  })
                                  prevPage.setData({
                                    dealFiles: that.data.dealFiles.concat(tempFilePath),
                                    imageUrl: that.data.imgUrl.concat(res4.data),
                                    hide : false
                                  })
                                }else{
                                  console.log("------!!!null-------")
                                  prevPage.setData({
                                    dealFiles: wx.getStorageSync('fileAndImg').files.concat(tempFilePath),
                                    imageUrl: wx.getStorageSync('fileAndImg').imgUrl.concat(res4.data),
                                    hide : false
                                  })
                                  wx.setStorageSync('fileAndImg', {
                                    files : wx.getStorageSync('fileAndImg').files.concat(tempFilePath),
                                    imgUrl : wx.getStorageSync('fileAndImg').imgUrl.concat(res4.data)
                                  })
                                  if(wx.getStorageSync('fileAndImg').files.length == 3){
                                    prevPage.setData({
                                      hide : true
                                    })
                                  }
                                }
                                wx.navigateBack({
                                  delta: 1
                                })
                                wx.showToast({
                                  title: "上传成功"
                                })
                              }
                            },
                            fail: res4 => {
                              console.log(res4)
                              wx.showToast({
                                title: "图片上传失败",
                                image: "/images/error.png"
                              })
                            }
                          })
                      },
                      fail: function (res3) {
                          console.log(res3);
                      }
                  });
                 },500);
              } else if(that.data.type == 6) {
                // 巡查
                let rowArr = that.transformation(that.data.recordContent)
                let k = 0
                if(rowArr.length < 3){
                  if(rowArr.length == 0){
                    k = 1
                  }else{
                    k = rowArr.length
                  }
                }else{
                  k = 3
                }
                ctx.drawImage(res2.path,0,0,width,height)
                ctx.rect(10, (height-230+(3-k)*20), 207, 50)
                ctx.setFillStyle('#396EB0')
                ctx.fill()
                ctx.beginPath()
                ctx.rect(10, (height-180+(3-k)*20), 207, 170-(3-k)*20)
                ctx.setFillStyle('#EAECED')
                ctx.fill()
                ctx.beginPath()
                ctx.setFontSize(16)
                ctx.setFillStyle('#ffffff')
                ctx.fillText('隐患记录', 20, (height-210+(3-k)*20))
                ctx.beginPath()
                ctx.setFontSize(13)
                ctx.setFillStyle('#ffffff')
                ctx.fillText(datetime, 80, (height-188+(3-k)*20))
                ctx.beginPath()
                ctx.setFontSize(13)
                ctx.setFillStyle('black')
                ctx.fillText('上', 20, (height-160+(3-k)*20))
                ctx.fillText('报', 40, (height-160+(3-k)*20))
                ctx.fillText('人', 60, (height-160+(3-k)*20))
                ctx.fillText(app.globalData.realname, 85, (height-160+(3-k)*20))
                ctx.fillText('地', 20, (height-135+(3-k)*20))
                ctx.fillText('点', 60, (height-135+(3-k)*20))
                let rowArr2 = that.transformation(that.data.address)
                for (let i = 0; i < rowArr2.length; i++) {
                  if(i == 0){
                    if(rowArr2.length > 1){
                      ctx.fillText(rowArr2[i].slice(0,9).concat("..."), 85, (height-135+(3-k)*20)+i*20);
                    }else{
                      ctx.fillText(rowArr2[i], 85, (height-135+(3-k)*20)+i*20)
                    }
                  }
                }
                ctx.fillText('隐患类别', 20, (height-110+(3-k)*20));
                ctx.fillText(that.data.category, 85, (height-110+(3-k)*20))
                ctx.fillText('隐患等级', 20, (height-85+(3-k)*20));
                ctx.fillText(that.data.level, 85, (height-85+(3-k)*20))
                ctx.fillText('隐患描述', 20, (height-60+(3-k)*20));
                for (let i = 0; i < rowArr.length; i++) {
                    if(i == 0){
                      ctx.fillText(rowArr[i], 85, (height-60+(3-k)*20)+i*20);
                    }else if(i == 1){
                      ctx.fillText(rowArr[i], 85, (height-60+(3-k)*20)+i*20);
                    }else if(i == 2){
                      if(rowArr.length > 3){
                        ctx.fillText(rowArr[i].slice(0,9).concat("..."), 85, (height-60+(3-k)*20)+i*20);
                      }else{
                        ctx.fillText(rowArr[i], 85, (height-60+(3-k)*20)+i*20);
                      }
                    }
                }
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-157+(3-k)*20))
                ctx.lineTo(212, (height-157+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-132+(3-k)*20))
                ctx.lineTo(212, (height-132+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-107+(3-k)*20))
                ctx.lineTo(212, (height-107+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-82+(3-k)*20))
                ctx.lineTo(212, (height-82+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-57+(3-k)*20))
                ctx.lineTo(212, (height-57+(3-k)*20))
                ctx.stroke()
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-37+(3-k)*20))
                ctx.lineTo(212, (height-37+(3-k)*20))
                ctx.stroke() 
                ctx.beginPath()
                ctx.setStrokeStyle('#A1A1A2')
                ctx.moveTo(84, (height-17+(3-k)*20))
                ctx.lineTo(212, (height-17+(3-k)*20))
                ctx.stroke() 
                ctx.draw()
                setTimeout(function(){
                  wx.canvasToTempFilePath({
                      canvasId: 'myCanvas',
                      success: function (res3) {
                          var tempFilePath = res3.tempFilePath;
                          console.log(tempFilePath)
                          //图片上传功能
                          wx.uploadFile({
                            url: app.inspectUrl + '/WeChat/weChatSystem/fileUpload',
                            // filePath: res.tempFilePaths[i],
                            filePath: tempFilePath,
                            name: 'file',
                            header: {
                              "Content-Type": "multipart/form-data"
                            },
                            success: res4 => {
                              console.log("res4",res4.data)
                              if (res.statusCode == 500) {
                                wx.showToast({
                                  title: "图片上传失败",
                                  image: "/images/error.png"
                                })
                              } else{
                                var pages = getCurrentPages();
                                var prevPage = pages[pages.length - 2];
                                const contentIndex = that.data.contentIndex
                                let dealFilesStr = 'superviseContentList['+ contentIndex +'].dealFiles'
                                let imgUrlStr = 'superviseContentList['+ contentIndex +'].imageUrl'
                                let hideStr = 'superviseContentList['+ contentIndex +'].hide'
                                if(wx.getStorageSync('fileAndImg-'+contentIndex) == null || wx.getStorageSync('fileAndImg-'+contentIndex) == ""){
                                  wx.setStorageSync('fileAndImg-'+contentIndex, {
                                      dealFiles : that.data.files.concat(tempFilePath),
                                      imageUrl : that.data.imgUrl.concat(res4.data),
                                  })
                                  prevPage.setData({
                                      [dealFilesStr]: that.data.files.concat(tempFilePath),
                                      [imgUrlStr]: that.data.imgUrl.concat(res4.data),
                                      [hideStr]: false
                                  })
                                }else{
                                  prevPage.setData({
                                    [dealFilesStr]: wx.getStorageSync('fileAndImg-'+contentIndex).dealFiles.concat(tempFilePath),
                                    [imgUrlStr]: wx.getStorageSync('fileAndImg-'+contentIndex).imageUrl.concat(res4.data),
                                    [hideStr]: false
                                  })
                                  wx.setStorageSync('fileAndImg-'+contentIndex, {
                                    dealFiles : wx.getStorageSync('fileAndImg-'+contentIndex).dealFiles.concat(tempFilePath),
                                    imageUrl : wx.getStorageSync('fileAndImg-'+contentIndex).imageUrl.concat(res4.data)
                                  })
                                  if(wx.getStorageSync('fileAndImg-'+contentIndex).dealFiles.length >= 3){
                                    prevPage.setData({
                                      [hideStr]: true
                                    })
                                  }
                                }
                                wx.navigateBack({
                                  delta: 1
                                })
                                wx.showToast({
                                  title: "上传成功"
                                })
                              }
                            },
                            fail: res4 => {
                              console.log(res4)
                              wx.showToast({
                                title: "图片上传失败",
                                image: "/images/error.png"
                              })
                            }
                          })
                      },
                      fail: function (res3) {
                          console.log(res3);
                      }
                  });
                 },500);
              }
            }
          })
        }
    })
  },
  error(e) {
    console.log(e.detail)
  },
 
})

